﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace PTA.Db
{
    #region 访问OleDb的数据库的命令
    /// <summary>
    /// 访问OleDb的数据库的命令
    /// </summary>
    public class OleDbDataCommand : DataCommand<OleDbParameter>
    {
        private const string PARAMETERPREFIX = "@";

        protected override string ParameterPrefix
        {
            get { return PARAMETERPREFIX; }
        }
        public OleDbDataCommand()
        {
        }

        public OleDbDataCommand(string commandText)
            : base(commandText)
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commandText">数据库命令文本</param>
        /// <param name="commandType">数据库命令类型</param>
        /// <param name="parameters">数据库命令参数</param>
        public OleDbDataCommand(string commandText, CommandType commandType, OleDbParameter[] parameters) :
            base(commandText, commandType, parameters)
        {
        }

        public void AddIn(string name, OleDbType type)
        {
            this.Add(new OleDbParameter(name, type));
        }

        public void AddIn(string name, OleDbType type, int size)
        {
            this.Add(new OleDbParameter(name, type, size));
        }

        public void AddIn(string name, OleDbType type, int size, object value)
        {
            this.Add(new OleDbParameter(name, type, size) { Value = value });
        }

        public void AddIn(string name, OleDbType type, object value)
        {
            this.Add(new OleDbParameter(name, type) { Value = value });
        }

        public void Add(string name, object value, OleDbType type, ParameterDirection direction, int size)
        {
            this.Add(new OleDbParameter(name, type, size) { Direction = direction, Value = value });
        }

        public void AddOut(string name, OleDbType type)
        {
            this.parameters.Add(new OleDbParameter(name, type) { Direction = ParameterDirection.Output });
        }

        public void AddOut(string name, OleDbType type, int size)
        {
            this.parameters.Add(new OleDbParameter(name, type, size) { Direction = ParameterDirection.Output });
        }

        public void AddReturn(string name, OleDbType type)
        {
            this.parameters.Add(new OleDbParameter(name, type) { Direction = ParameterDirection.ReturnValue });
        }

        public void AddReturn(string name, OleDbType type, int size)
        {
            this.parameters.Add(new OleDbParameter(name, type, size) { Direction = ParameterDirection.ReturnValue });
        }

    }

    public class OleDbPageSearch : OleDbDataCommand, IPageSearch<OleDbParameter>
    {

        #region 查询字段
        private string fieldsName = "*";
        /// <summary>
        /// 查询字段(如果不设置,默认返回*)
        /// </summary>
        public string FieldsName
        {
            get
            {
                return this.fieldsName;
            }
            set
            {
                this.fieldsName = (value ?? "*").Trim();
            }
        }
        #endregion

        #region 表名称
        private string tableName = string.Empty;
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName
        {
            get
            {
                return this.tableName;
            }
            set
            {
                this.tableName = (value ?? string.Empty).Trim();
            }
        }
        #endregion

        #region 主键字段
        private string primaryKey = string.Empty;
        /// <summary>
        /// 主键字段
        /// </summary>
        public string PrimaryKey
        {
            get
            {
                return this.primaryKey;
            }
            set
            {
                this.primaryKey = (value ?? string.Empty).Trim();
            }
        }
        #endregion

        #region 排序字段

        private OrderFieldCollection orderField = new OrderFieldCollection();
        /// <summary>
        /// 排序字段
        /// </summary>
        public OrderFieldCollection OrderField
        {
            get
            {
                return this.orderField;
            }
        }
        #endregion

        public void And(string sql, params string[] args)
        {
            throw new NotImplementedException();
        }
        public string Where
        {
            get
            {
                return string.Empty;
            }
        }
        #region 当前页码
        private int pageIndex = 0;
        /// <summary>
        /// 当前页码(从0开始)
        /// </summary>
        public int PageIndex
        {
            get
            {
                return this.pageIndex;
            }
            set
            {
                this.pageIndex = value > 0 ? value : 0;
            }
        }
        #endregion

        #region 每页数量
        private int pageSize = 15;
        /// <summary>
        /// 每页数量(默认15),等于0时,查询全部
        /// </summary>
        public int PageSize
        {
            get
            {
                return pageSize;
            }
            set
            {
                if (value < 0)
                {
                    pageSize = Math.Abs(value);
                }
                else
                {
                    pageSize = value;
                }
            }
        }
        #endregion

        #region 记录总数
        private int totalCount;
        /// <summary>
        /// 记录总数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return this.totalCount;
            }
              set
            {
                this.totalCount = value > 0 ? value : 0;
            }
        }
        #endregion

        #region 页数
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                if (PageSize == 0)
                {
                    return 1;
                }
                return (totalCount + pageSize - 1) / pageSize;
            }
        }
        #endregion

    }
    #endregion

    /// <summary>
    /// OleDb数据库访问类
    /// </summary>
    public class OleDbDataAccess : DataAccess<OleDbConnection, OleDbDataAdapter, OleDbTransaction, OleDbParameter, OleDbDataCommand, OleDbPageSearch>
    {
        private const string PARAMETERPREFIX = "@";
        public override string ParameterPrefix
        {
            get
            {
                return PARAMETERPREFIX;
            }
        }
        /// <summary>
        /// 默认的构造函数
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        public OleDbDataAccess(string connectionString)
            : base(connectionString)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <
        public OleDbDataAccess(string connectionString,bool enablelog)
            : base(connectionString)
        {
            this.WriteDebug = enablelog;
        }


        /// <summary>
        /// 默认的构造函数
        /// </summary>
        /// <param name="connectionBuilder">数据库连接字符串生成器</param>
        public OleDbDataAccess(OleDbConnectionStringBuilder connectionBuilder) :
            this(connectionBuilder.ConnectionString)
        {
        }

        /// <summary>
        /// 获取存储过程的参数
        /// </summary>
        /// <param name="procName">存储过程名称</param>
        /// <returns>存储过程的参数</returns>
        protected override OleDbParameter[] GetProcParameters(string procName)
        {
            OleDbParameter[] pvs = GrabParameters(procName);
            if (pvs == null)
            {
                using (OleDbCommand cmd = new OleDbCommand(procName, CreateConnection()))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection.Open();
                    OleDbCommandBuilder.DeriveParameters(cmd);
                    cmd.Connection.Dispose();
                    cmd.Parameters.RemoveAt(0);
                    pvs = new OleDbParameter[cmd.Parameters.Count];
                    cmd.Parameters.CopyTo(pvs, 0);
                    SaveParameters(procName, pvs);
                    pvs = GrabParameters(procName);
                }
            }
            return pvs;
        }

        public override void Insert(BaseEntity m)
        {
            throw new NotImplementedException();
        }

        public override int Update(BaseEntity m)
        {
            throw new NotImplementedException();
        }

        #region 参数管理

        /// <summary>
        /// 创建数据库命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="type">参数类型</param>
        /// <returns>数据库命令参数</returns>
        public OleDbParameter MakeIn(string name, OleDbType type)
        {
            return new OleDbParameter(name, type);
        }

        /// <summary>
        /// 创建数据库命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="size">参数长度</param>
        /// <param name="type">参数类型</param>
        /// <returns>数据库命令参数</returns>
        public OleDbParameter MakeIn(string name, OleDbType type, int size)
        {
            return new OleDbParameter(name, type, size);
        }

        /// <summary>
        /// 创建数据库命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="value">参数值</param>
        /// <param name="type">参数类型</param>
        /// <param name="size">参数长度</param>
        /// <returns>数据库命令参数</returns>
        public OleDbParameter MakeIn(string name, object value, OleDbType type, int size)
        {
            OleDbParameter p = new OleDbParameter(name, type, size);
            p.Value = value;
            return p;
        }

        /// <summary>
        /// 创建数据库命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="type">参数类型</param>
        /// <returns>数据库命令参数</returns>
        public OleDbParameter MakeOut(string name, OleDbType type)
        {
            OleDbParameter p = new OleDbParameter(name, type);
            p.Direction = ParameterDirection.Output;
            return p;
        }

        /// <summary>
        /// 创建数据库命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="size">参数长度</param>
        /// <param name="type">参数类型</param>
        /// <returns>数据库命令参数</returns>
        public OleDbParameter MakeOut(string name, OleDbType type, int size)
        {
            OleDbParameter p = new OleDbParameter(name, type, size);
            p.Direction = ParameterDirection.Output;
            return p;
        }

        /// <summary>
        /// 创建数据库命令参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="value">参数值</param>
        /// <param name="type">参数类型</param>
        /// <param name="size">参数长度</param>
        /// <param name="direction">参数方向</param>
        /// <returns>数据库命令参数</returns>
        public OleDbParameter MakeParam(string name, object value, OleDbType type, ParameterDirection direction, int size)
        {
            OleDbParameter p = new OleDbParameter(name, type);
            if (value != null)
            {
                p.Value = value;
            }
            if (size > 0)
            {
                p.Size = size;
            }
            p.Direction = direction;
            return p;
        }

        #endregion
    }

}
